package com.zhoug.share.core;

import android.content.Context;
import android.os.Environment;


import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.FileUtils;
import com.zhoug.common.utils.IOUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ThreadUtils;
import com.zhoug.share.core.ins.ShareCallback;
import com.zhoug.share.core.ins.ShareClient;

import java.io.File;
import java.util.HashMap;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.weibo.SinaWeibo;

/**
 * 极光分享
 *
 * @Author HK-LJJ
 * @Date 2019/12/25
 * @Description
 */
class JShareClient implements ShareClient {
    private static final String TAG = ">>>JShareClient";

    private ShareCallback shareCallback;


    public JShareClient() {
    }

    public JShareClient(ShareCallback shareCallback) {
        this.shareCallback = shareCallback;
    }


    /**
     * 微信分享
     *
     * @param shareType {@link #SHARE_TYPE_IMAGE}
     * @param filePath
     */
    @Override
    public void wechatShare(int shareType, String filePath) {
        if (JShareInterface.isClientValid(Wechat.Name)) {
            if (shareType == SHARE_TYPE_IMAGE) {
                ShareParams shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImagePath(filePath);
                JShareInterface.share(Wechat.Name, shareParams, platActionListener);
            }
        } else {
            LogUtils.e(TAG, "微信分享无效");
            if (shareCallback != null) {
                shareCallback.onError(ClientType.Wechat, 0, "微信分享无效");
            }
        }
    }

    /**
     * qq分享
     *
     * @param shareType {@link #SHARE_TYPE_IMAGE}
     * @param filePath
     */
    @Override
    public void qqShare(int shareType, String filePath) {
        if (JShareInterface.isClientValid(QQ.Name)) {
            if (shareType == SHARE_TYPE_IMAGE) {
                ShareParams shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImagePath(filePath);
                JShareInterface.share(QQ.Name, shareParams, platActionListener);
            }
        } else {
            LogUtils.e(TAG, "QQ分享无效");
            if (shareCallback != null) {
                shareCallback.onError(ClientType.QQ, 0, "QQ分享无效");
            }
        }
    }


    /**
     * 新浪微博分享
     *
     * @param shareType {@link #SHARE_TYPE_IMAGE}
     * @param filePath
     */
    @Override
    public void sinaWeiboShare(int shareType, String filePath) {
        if (JShareInterface.isClientValid(SinaWeibo.Name)) {
            if (shareType == SHARE_TYPE_IMAGE) {
                ShareParams shareParams = new ShareParams();
                shareParams.setShareType(Platform.SHARE_IMAGE);
                shareParams.setImagePath(filePath);
                JShareInterface.share(SinaWeibo.Name, shareParams, platActionListener);
            }
        } else {
            LogUtils.e(TAG, "新浪微博分享无效");
            if (shareCallback != null) {
                shareCallback.onError(ClientType.SinaWeibo, 0, "新浪微博分享无效");
            }
        }
    }

    /**
     * 本地保存
     *
     * @param shareType {@link #SHARE_TYPE_IMAGE}
     * @param filePath
     */
    @Override
    public void localShare(Context context, int shareType, String filePath) {

        File folder = FileUtils.getExternalPublicFolder(Environment.DIRECTORY_PICTURES);
        if (folder != null) {
            new Thread(() -> {
                File resultFile = new File(folder, FileUtils.getNameWithSuffix(filePath));
                final String absolutePath = resultFile.getAbsolutePath();
                final boolean success = IOUtils.copyFile(filePath, absolutePath);
                if (success) {
                    LogUtils.d(TAG, "保存成功");
                    AppUtils.scannerFile(context, absolutePath);
                    ThreadUtils.runMainThread(() -> {
                        if (shareCallback != null) {
                            shareCallback.onComplete(ClientType.Local, 0);
                        }
                    });
                } else {
                    LogUtils.e(TAG, "保存失败");
                    ThreadUtils.runMainThread(() -> {
                        if (shareCallback != null) {
                            shareCallback.onError(ClientType.Local, 0, "保存失败");
                        }

                    });
                }
            }).start();

        } else {
            LogUtils.e(TAG, "保存失败");
            if (shareCallback != null) {
                shareCallback.onError(ClientType.Local, 0, "保存失败");
            }
        }
    }


    /**
     * 分享回掉接口
     */
    private PlatActionListener platActionListener = new PlatActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            String name = platform.getName();
            LogUtils.d(TAG, "onComplete: i=" + i + ",name=" + name);
            if (shareCallback != null) {
                shareCallback.onComplete(getClientType(name), 0);
            }
        }

        @Override
        public void onError(Platform platform, int i, int i1, Throwable throwable) {
            String name = platform.getName();
            LogUtils.e(TAG, "onError: " + throwable.getMessage() + ",name=" + name);
            if (shareCallback != null) {
                shareCallback.onError(getClientType(name), 0, throwable.getMessage());
            }
        }

        @Override
        public void onCancel(Platform platform, int i) {
            String name = platform.getName();
            LogUtils.d(TAG, "onCancel: i=" + i + ",name=" + name);
            if (shareCallback != null) {
                shareCallback.onCancel(getClientType(name), 0);
            }
        }
    };

    /**
     * 根据名字获取ClientType
     *
     * @param name
     * @return
     */
    private ClientType getClientType(String name) {
        if (Wechat.Name.equals(name)) {
            return ClientType.Wechat;
        } else if (QQ.Name.equals(name)) {
            return ClientType.QQ;
        } else if (SinaWeibo.Name.equals(name)) {
            return ClientType.SinaWeibo;
        }
        return ClientType.Local;
    }

    @Override
    public ShareCallback getShareCallback() {
        return shareCallback;
    }

    @Override
    public void setShareCallback(ShareCallback shareCallback) {
        this.shareCallback = shareCallback;
    }

}
